package pl.perseus.dao;

import java.util.List;
import java.util.Map;

import org.mybatis.spring.support.SqlSessionDaoSupport;

public class BaseDao<T> extends SqlSessionDaoSupport{

	private final String prefix;
	
	public BaseDao(String prefix){
		this.prefix = prefix;
	}
	
	private String constructQueryId(String queryId){
		return prefix+"."+queryId;
	}
	
	public Object queryForPureObject(String queryId, Map<String, Object> map){
		return getSqlSession().selectOne(constructQueryId(queryId), map);
	}
	
	public T queryForObject(String queryId, Map<String, Object> map){
		return getSqlSession().selectOne(constructQueryId(queryId), map);
	}
	
	public List<T> queryForList(String queryId, Map<String, Object> map){
		return getSqlSession().selectList(constructQueryId(queryId), map);
	}
	
	public T insertBean(String queryId, T bean){
		getSqlSession().insert(constructQueryId(queryId), bean);
		return bean;
	}
	
	public T updateBean(String queryId, T bean){
		getSqlSession().update(constructQueryId(queryId), bean);
		return bean;
	}
	
	public void delete(String queryId, T bean){
		getSqlSession().delete(queryId, bean);
	}
	
}
